#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>  
#include <sys/types.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>
#include "ctl_car_msg.h"
#include "ctl_hand_msg.h"
#include "thirdparty/cJSON/cJSON.h"
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 9995
#define BUF_SIZE 1024

//g++ -lpthread -o client client.c 
int getSocket(){
    int fd =socket( AF_INET, SOCK_STREAM, 0 );
    printf("fd=%d\n",fd);
    if(-1 == fd){
        printf("Error, fd is -1\n");
    }
    return fd;
}

/**
 * 连接到server端，如果成功，返回fd，如果失败返回-1
 */
int connectServer(){
    int fd = getSocket();
    if(-1 == fd){
        return -1;
    }
    struct sockaddr_in remote_addr; //服务器端网络地址结构体
    memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零
    remote_addr.sin_family=AF_INET; //设置为IP通信
    remote_addr.sin_addr.s_addr=inet_addr(SERVER_IP);//服务器IP地址
    remote_addr.sin_port=htons(SERVER_PORT); //服务器端口号
    int con_result = connect(fd, (struct sockaddr*) &remote_addr, sizeof(struct sockaddr));
    if(con_result < 0){
        printf("Connect Error!\n");
        return -1;
    }
    printf("con_result=%d\n",con_result);
    return fd;
}

/**
*** 连接到server端，并且不断往server端发送心跳数据
*/
void* heartBeat(void* arg){
	int len_moveon,len_stop,write_num,read_num,ret;
	/*
    ctl_car_msg* moveon_msg;
    char* buf_moveon ;
	ctl_car_msg* stop_msg;
    char* buf_stop ;

	moveon_msg = create_ctl_car_msg(MSG_TYPE_CTL_CAR,1,CTL_CAR_CMD_MOVE_ON,50);
	ret = serialize_ctlcar_msg(moveon_msg,&buf_moveon,&len_moveon);
	
	stop_msg = create_ctl_car_msg(MSG_TYPE_CTL_CAR,1,CTL_CAR_CMD_STOP,50);
	ret = serialize_ctlcar_msg(stop_msg,&buf_stop,&len_stop);
	*/


	ctl_hand_msg * moveon_msg;
    char* buf_moveon ;
	ctl_hand_msg* stop_msg;
    char* buf_stop ;

	motion_data motion1[2] = {{0,800,800,0}, {1,2300,1000,0}};
	//motion_data motion1 = {0,600,800,0};
	motion_data motion2[2] = {{0,2200,1000,0},{1,1100,1000,0}};
	//typedef struct motion_data_{
	//	int motor;					//电机编号
	//	int postion;				//机械手电机转动到的位置
	//	int speed;					//机载手转动的速度
	//	int time;					//机械手电机转动到指定位置所需时间(单位毫秒)
	//}motion_data;
    //static char* CTL_HAND_MSG = "{\"user\":%d,\"cmd\":%d,\"hands_data\":[{\"motor\":%d,\"postion\":%d,\"speed\":%d,\"time\":%d}]}";
	//static char* CTL_HANDS_MSG = "{\"user\":%d,\"cmd\":%d,\"hands_data\":[{\"motor\":%d,\"postion\":%d,\"speed\":%d,\"time\":%d}";//]}";

	moveon_msg = create_ctl_hand_msg(MSG_TYPE_CTL_CAR_HAND,1,CTL_CAR_CMD_CTL_MOTION,2,motion1,0);
	ret = serialize_ctl_hand_msg(moveon_msg,&buf_moveon,&len_moveon);

	stop_msg = create_ctl_hand_msg(MSG_TYPE_CTL_CAR_HAND,1,CTL_CAR_CMD_CTL_MOTION,2,motion2,0);
	ret = serialize_ctl_hand_msg(stop_msg,&buf_stop,&len_stop);

	
    printf("Heart bead started!\n");
    int fd = connectServer();
    if(fd<0){
        printf("Heart bead quit!\n");
        return 0;
    }
    //char buf_moveon[BUF_SIZE];  //数据传送的缓冲区
    char buf_moveon_read[BUF_SIZE];
	char buf_stop_read[BUF_SIZE];
    //string value = "This is heart beat msg!";
    //strcpy(buf_moveon, value.c_str());    
    while(1){
        printf("moveon:to write data:%.*s",len_moveon,buf_moveon);
        write_num = write(fd, buf_moveon, len_moveon);
        printf("moveon:write_num=%d\n",write_num);
		printfmsg(buf_moveon,len_moveon);
        read_num = read(fd,buf_moveon_read,BUF_SIZE);
        if(read_num>0){
                printf("moveon:read data len:%d\n",read_num);
				printfmsg(buf_moveon_read,read_num);
                memset(buf_moveon_read,0,BUF_SIZE);
        }       
        sleep(5);

		printf("stop:to write data:%.*s",len_stop,buf_stop);
        write_num = write(fd, buf_stop, len_stop);
        printf("stop:write_num=%d\n",write_num);
		printfmsg(buf_stop,len_stop);
        read_num = read(fd,buf_stop_read,BUF_SIZE);
        if(read_num>0){
                printf("stop:read data len:%d\n",read_num);
				printfmsg(buf_stop_read,read_num);
                memset(buf_stop_read,0,BUF_SIZE);
        } 
		sleep(5);
    }
    if(buf_moveon) free(buf_moveon);
    if(moveon_msg) distory_ctl_car_msg(moveon_msg);
	if(buf_stop) free(buf_stop);
    if(stop_msg) distory_ctl_car_msg(stop_msg);
    printf("Heart bead finished!\n");
    return 0;
}


int main() {
    pthread_t thread;
    printf("-------------------------");
    printf( "main started\n"); // prints Hello World!!!
    pthread_create(&thread,NULL,heartBeat,NULL);
    //pthread_detach(thread);
    sleep(10000000);
   
    printf("sleep 10 s over\n");
    pthread_join(thread,0);
    printf("main end\n");
    return 0;
}
